﻿/// <binding />
module.exports = function (grunt) {
    grunt.initConfig({
        htmlConvert: {
            options: {
                rename: function (moduleName) {
                    var name = moduleName.replace('.html', '');
                    var index = name.lastIndexOf("/");
                    return name.substring(index + 1);
                }
            },
            templates: {
                src: ['Components/*.html'],
                dest: 'bundels/templates.js'
            }
        },
        concat: {
                options: {
            
                },
                css: {
                    src: [
                        "bower_components/bootstrap/dist/css/bootstrap.min.css"
                    ],
                    dest: 'bundels/styles.css'
                },
                libs: {
                    src: [
                        "bower_components/jquery/dist/jquery.min.js",
                        "bower_components/underscore/underscore-min.js",
                        "bower_components/tether/dist/js/tether.min.js",
                        "bower_components/bootstrap/dist/js/bootstrap.min.js",
                        "bower_components/vue/dist/vue.min.js",
                        "bower_components/vue-form/vue-form.min.js"
                    ],
                    dest: 'bundels/libs.js'
                },
                scripts: {
                    src: [
                        "Shared/*.js",                        
                        "Services/*.js",
                        "Directives/*.js",
                        "Collections/*.js"
                    ],
                    dest: 'bundels/scripts.js'
                },
                components: {
                    src: [
                        "Components/*.js",
                        "index.js"
                    ],
                    dest: 'bundels/components.js'
                }
        },
        uglify: {  
            options: {  
                compress: true
            },  
            min: {
                files: grunt.file.expandMapping(['bundels/*.js'], '', {
                    rename: function (destBase, destPath) {
                        return destBase + destPath.replace('.js', '.min.js');
                    }
                })
            }
        },
        cssmin: {
            target: {
                files: [{
                    expand: true,
                    cwd: 'bundels',
                    src: ['*.css', '!*.min.css'],
                    dest: 'bundels',
                    ext: '.min.css'
                }]
            }
        },
        cacheBust: {
            hashOrginalFileNamesVersion: {
                options: {
                    assets: ['bundels/*.js', 'bundels/*.css']
                    
                },
                src: ['index.html']
            },
            hashMinFileNamesVersion: {
                options: {
                    assets: ['bundels/*.min.js', 'bundels/*.min.css']

                },
                src: ['index.html']
            }
        },
        clean: {
            bundels: ['bundels/*'],
        },        
        replace: {
                resetFileReferenceDev: {
                                src: ['index.html'],
                                overwrite: true,
                                replacements: [
                                    {                                                                                
                                        from: /(src="bundels\/[^\.]+)(\.min)*(\.[a-z0-9]+)*(?=\.js)/g,
                                        to: "$1"
                                    },
                                    {
                                        from: /(href="bundels\/[^\.]+)(\.min)*(\.[a-z0-9]+)*(?=\.css)/g,
                                        to: "$1"
                                    }
                                ]
                },
                resetFileReferenceProd: {
                    src: ['index.html'],
                    overwrite: true,
                    replacements: [
                        {
                            from: /(src="bundels\/[^\.]+)(\.min)*(\.[a-z0-9]+)*(?=\.js)/g,
                            to: "$1.min"
                        },
                        {
                            from: /(href="bundels\/[^\.]+)(\.min)*(\.[a-z0-9]+)*(?=\.css)/g,
                            to: "$1.min"
                        }
                    ]
                }
        },
        watch: {                        
            js: {
                files: ['Components/*.js', 'Shared/*.js', "Services/*.js", "Collections/*.js", "Directives/*.js", "index.js"],
                tasks: ['concat']
            },
            html: {
                files: ['Components/*.html'],
                tasks: ['htmlConvert']
            }
        },
    });    
    
    grunt.loadNpmTasks('grunt-html-convert');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-cache-bust');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-text-replace');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-watch');    

    grunt.registerTask('development', ['clean', 'replace:resetFileReferenceDev', 'concat', 'htmlConvert']);
    grunt.registerTask('production', ['clean', 'replace:resetFileReferenceProd', 'concat', 'htmlConvert', 'uglify', 'cssmin', 'cacheBust:hashMinFileNamesVersion']);    
};